var app = getApp(),
  that,
  util = require("../../../../utils/util.js");

Page({
  data: {
    tabs: ["可用的", "失效的"],
    activeIndex: 0,
    items1: [],
    keyong: [],
    items: [],
    shixiao: [],
    startX: 0,
    startY: 0,
    showModal: !1,
    memberId: {},
    isLoadMore: true,
    isJzz: false,
    isJzwb: false,
    page: 1,
    rows: 10,
  },
  // 立即购买二维码
  ljsy: function(res) {
    console.log(res)
    wx.navigateTo({
      url: 'hxq?item=' + res.currentTarget.dataset.cardid
    });
  },
  tabClick: function(t) {
    this.setData({
      activeIndex: t.currentTarget.id
    });
  },
  yczz: function() {
    this.setData({
      showModal: !1
    });
  },
  onReady: function() {},
  onShow: function() {
    that = this;
    that.setData({
      memberId: wx.getStorageSync('bizUserInfo').id
    });
    that.getKeyong();
    that.getShixiao()
  },
  // 可用的次卡
  getKeyong() {
    app.getData({
      url: app.api.getWdck(that.data.memberId, 1, that.data.page, that.data.rows),
      method: 'GET',
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            // that.setData({
            //   keyong: res.data.list
            // })
            if (that.data.page == 1) {
              that.data.keyong = []
            }
            if (res.data.list.length < that.data.rows) {
              that.setData({
                keyong: that.data.keyong.concat(res.data.list),
                isLoadMore: false,
                isJzz: true
              })
            } else {
              that.setData({
                keyong: that.data.keyong.concat(res.data.list),
                isLoadMore: true,
                page: that.data.page + 1,
              })
            }
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        }
      }

    })
  },
  // 失效的
  getShixiao() {
    app.getData({
      url: app.api.getWdck(that.data.memberId, 2, 1, 10),
      method: 'GET',
      ok: function(res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {

            if (that.data.page == 1) {
              that.data.shixiao = []
            }
            if (res.data.list.length < that.data.rows) {
              that.setData({
                shixiao: that.data.shixiao.concat(res.data.list),
                isLoadMore: false,
                isJzz: true
              })
            } else {
              that.setData({
                shixiao: that.data.shixiao.concat(res.data.list),
                isLoadMore: true,
                page: that.data.page + 1,
              })
            }
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        }
      }

    })
  },
  onHide: function() {},
  onUnload: function() {},
  onReachBottom: function() {},
  touchstart1: function(t) {
    this.data.items1.forEach(function(t, a) {
      t.isTouchMove && (t.isTouchMove = !1);
    }), this.setData({
      startX: t.changedTouches[0].clientX,
      startY: t.changedTouches[0].clientY,
      items1: this.data.items1
    });
  },
  touchmove1: function(t) {
    var a = this,
      e = t.currentTarget.dataset.index,
      o = a.data.startX,
      s = a.data.startY,
      n = t.changedTouches[0].clientX,
      i = t.changedTouches[0].clientY,
      c = a.angle({
        X: o,
        Y: s
      }, {
        X: n,
        Y: i
      });
    a.data.items1.forEach(function(t, a) {
      t.isTouchMove = !1, 30 < Math.abs(c) || a == e && (t.isTouchMove = !(o < n));
    }), a.setData({
      items1: a.data.items1
    });
  },
  touchstart: function(t) {
    this.data.items.forEach(function(t, a) {
      t.isTouchMove && (t.isTouchMove = !1);
    }), this.setData({
      startX: t.changedTouches[0].clientX,
      startY: t.changedTouches[0].clientY,
      items: this.data.items
    });
  },
  touchmove: function(t) {
    var a = this,
      e = t.currentTarget.dataset.index,
      o = a.data.startX,
      s = a.data.startY,
      n = t.changedTouches[0].clientX,
      i = t.changedTouches[0].clientY,
      c = a.angle({
        X: o,
        Y: s
      }, {
        X: n,
        Y: i
      });
    a.data.items.forEach(function(t, a) {
      t.isTouchMove = !1, 30 < Math.abs(c) || a == e && (t.isTouchMove = !(o < n));
    }), a.setData({
      items: a.data.items
    });
  },
  angle: function(t, a) {
    var e = a.X - t.X,
      o = a.Y - t.Y;
    return 360 * Math.atan(o / e) / (2 * Math.PI);
  },
  del: function(t) {
    var a = this,
      e = t.currentTarget.dataset.yhqid;
    console.log(t, e), wx.showModal({
      title: "提示",
      content: "确认删除此券吗？",
      success: function(t) {
        t.confirm ? (console.log("用户点击确定"), app.util.request({
          url: "entry/wxapp/DelCoupons",
          cachetime: "0",
          data: {
            id: e
          },
          success: function(t) {
            console.log(t.data), 1 == t.data ? (wx.showToast({
              title: "删除成功",
              icon: "success",
              duration: 1e3
            }), setTimeout(function() {
              a.reLoad();
            }, 1e3)) : wx.showToast({
              title: "请重试",
              icon: "loading",
              duration: 1e3
            });
          }
        })) : t.cancel && console.log("用户点击取消");
      }
    });
  },
  toSyjl: function(res) {
    wx.navigateTo({
      url: 'syjl?item=' + JSON.stringify(res.currentTarget.dataset.item)
    })
  },
  onReachBottom: function() {
    if (that.data.isLoadMore) {
      that.getKeyong();
      that.getShixiao();
    } else {
      that.setData({
        isJzwb: true,
        isJzz: true
      })
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },
});